package com.letv.android.client.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.letv.android.young.client.R;
import com.letv.core.utils.StringUtils;

/**
 * seekbar
 *
 * @author zhuqiao
 *
 */
@SuppressLint("NewApi") public class LetvSeekBar extends RelativeLayout implements OnSeekBarChangeListener {
    protected static final String TIME_NULL_MINUTE = "00:00";
    protected static final String TIME_NULL_HOUR = "00:00:00";

    protected Context mContext;
    protected OnSeekBarTouchListener mSeekBarTouchListener;
    protected OnSeekBarChangeListener mOnSeekBarChangeListener;

    protected SeekBar mSeekBar;
    /** 当前播放时间 **/
    protected TextView mBeginTextView;
    /** 结束时间 **/
    protected TextView mEndTextView;

    /** thumb **/
    protected Drawable mThumb;

    protected boolean mIsChangeShow;

    /** 记录首次测量的mEndTextView的宽度 **/
    protected int mEndTextViewWidth = -1;

    protected int mEndTextViewLeftMargin = 0;

    protected String mEndTimes = TIME_NULL_MINUTE;

    public interface OnSeekBarTouchListener {
        void onSeekBarTouch(MotionEvent motionEvent);
    }

    public LetvSeekBar(Context context) {
        this(context, null);
    }

    public LetvSeekBar(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.seekBarStyle);
    }

    public LetvSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init();
    }

    public void initBeginTextView() {
//    	LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mBeginTextView.getLayoutParams();
//        params.leftMargin = 0;
//        mBeginTextView.setLayoutParams(params);
        mBeginTextView.invalidate();
    }

    protected void init() {
        View frame = LayoutInflater.from(mContext).inflate(R.layout.letv_layout_seekbar, null);
        mSeekBar = (SeekBar) frame.findViewById(R.id.seekbar);
        mBeginTextView = (TextView) frame.findViewById(R.id.seek_start_time);
        mEndTextView = (TextView) frame.findViewById(R.id.seek_end_time);
        addView(frame);

        if (mThumb == null)
            mThumb = getResources().getDrawable(R.drawable.b1_seekbar);
        mSeekBar.setThumb(mThumb);
        mSeekBar.setThumbOffset(0);
        mSeekBar.setOnSeekBarChangeListener(this);
        mSeekBar.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        if (mSeekBarTouchListener != null) {
                            mSeekBarTouchListener.onSeekBarTouch(event);
                        }
                        break;
                }
                return false;
            }
        });
    }

    public int getMax() {
        return mSeekBar.getMax();
    }

    public int getProgress() {
        return mSeekBar.getProgress();
    }

    public void setEnable(boolean enabled) {
        mSeekBar.setEnabled(enabled);
    }

    public void setMax(int max) {
        mSeekBar.setMax(max);
    }

    public void setSeekBarVisiable(int visiable) {
        mSeekBar.setVisibility(visiable);
    }

    /**
     * 重置结束时间
     */
    public void resetEndTime() {
        if (mEndTextView != null) {
            mEndTextView.setText("");
        }
    }

    /**
     * 设置结束时间
     *
     */
    public void setEndTime(String totalTime) {
        if (mEndTextView != null) {
            mEndTextView.setText(totalTime);
        }
    }

    /**
     * 设置结束时间
     *
     * @param timeMs
     */
    public void setEndTime(long timeMs) {
        if (!mIsChangeShow) {
            mEndTextView.setVisibility(View.VISIBLE);
            mEndTimes = StringUtils.stringForTimeNoHour(timeMs);
            mEndTextView.setText(mEndTimes);
            if (TextUtils.equals(TIME_NULL_MINUTE, mEndTimes)) {
                mEndTextView.setVisibility(GONE);
            } else {
                mEndTextView.setVisibility(VISIBLE);
            }
            mEndTextViewLeftMargin = getMeasuredWidth() - mEndTextView.getMeasuredWidth();
        }
    }

    /**
     * 设置结束时间
     *
     * @param timeMs
     */
    public void setEndTextView(long timeMs) {
        mEndTimes = StringUtils.stringForTimeNoHour(timeMs);
        mEndTextView.setText(mEndTimes);
    }

    /**
     * 获取总共时间
     *
     * @return
     */
    public String getTotalTimes() {
        if (mEndTextView != null) {
            return mEndTextView.getText().toString();
        }

        return "";
    }

    /**
     * 设置开始时间
     *
     * @param timeMs
     */
    public void setBeginTime(long timeMs) {
        mBeginTextView.setVisibility(View.VISIBLE);
        String beginTime = StringUtils.stringForTimeNoHour(timeMs);
        mBeginTextView.setText(beginTime);
        if ("00:00".equals(beginTime)) {
            mBeginTextView.setVisibility(GONE);
        } else {
            mBeginTextView.setVisibility(VISIBLE);
        }
    }

    public void hideOrShowTime(boolean isShow) {
        hideOrShowBeginTime(isShow);
        hideOrShowEndTime(isShow);
    }

    public void hideOrShowBeginTime(boolean isShow) {
        if (mEndTextView != null && !TextUtils.isEmpty(mEndTextView.getText())) {
            if (String.valueOf(mEndTextView.getText()).contains("/")) {
                isShow = false;
            }
        }
        mBeginTextView.setVisibility(isShow ? View.VISIBLE : View.INVISIBLE);
    }

    public void hideOrShowEndTime(boolean isShow) {
        mEndTextView.setVisibility(isShow ? View.VISIBLE : View.INVISIBLE);
    }

    /**
     * 重置beginTextView
     */
//    public void reLayout() {
//    	LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mBeginTextView.getLayoutParams();
//        params.leftMargin = 0;
//        mBeginTextView.setLayoutParams(params);
//    }

    public void setProgress(int progress) {
        mSeekBar.setProgress(progress);
        mBeginTextView.setText(StringUtils.stringForTimeNoHour(progress * 1000));
    }

    public void setChangeShow(boolean isChangeShow) {
        this.mIsChangeShow = isChangeShow;
    }

    public void setSecondaryProgress(int secondaryProgress) {
        mSeekBar.setSecondaryProgress(secondaryProgress);
    }

    public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {
        mOnSeekBarChangeListener = l;
    }

    public void setOnSeekBarTouchListener(OnSeekBarTouchListener listener) {
        mSeekBarTouchListener = listener;
    }

    public Drawable getThumb() {
        return mThumb;
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        setBeginPos(progress);

        if (mOnSeekBarChangeListener != null)
            mOnSeekBarChangeListener.onProgressChanged(seekBar, progress, fromUser);
    }

    public void setBeginPos(int progress) {
        if (!isEnabled())
            return;

        if (getVisibility() == VISIBLE && !TextUtils.equals(TIME_NULL_HOUR, mEndTimes)) {
//            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mBeginTextView.getLayoutParams();

            if (mEndTextViewWidth == -1) {
                mEndTextViewWidth = mEndTextView.getMeasuredWidth();
            }
            mEndTextViewLeftMargin = getMeasuredWidth() - mEndTextViewWidth;
            if (mEndTextViewLeftMargin > 0 && mThumb.getBounds().right >= mEndTextViewLeftMargin) {
                mIsChangeShow = true;
                mBeginTextView.setVisibility(View.INVISIBLE);
//                params.leftMargin = 0;
//                mBeginTextView.setLayoutParams(params);
            } else {
                mIsChangeShow = false;
                mEndTextViewWidth = -1;
                mBeginTextView.setVisibility(View.VISIBLE);
//                int offset = mThumb.getBounds().left - mBeginTextView.getMeasuredWidth();
//                params.leftMargin = offset < 0 ? 0 : offset;
//                if (11 <= LetvUtils.getSDKVersion()) {
//                    mBeginTextView.setLeft(params.leftMargin);
//                }
                mBeginTextView.setText(StringUtils.stringForTimeNoHour(progress * 1000));
            }
            if (mBeginTextView.getVisibility() == View.INVISIBLE) {
                mEndTextView.setText(StringUtils.stringForTimeNoHour(progress * 1000) + "/" + mEndTimes);
            } else {
                mBeginTextView.setText(StringUtils.stringForTimeNoHour(progress * 1000));
                mEndTextView.setText(mEndTimes);
            }
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        if (isEnabled()) {
            if (mOnSeekBarChangeListener != null) {
                mOnSeekBarChangeListener.onStartTrackingTouch(mSeekBar);
            }
        }
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if (isEnabled()) {
            if (mOnSeekBarChangeListener != null) {
                mOnSeekBarChangeListener.onStopTrackingTouch(mSeekBar);
            }
        }
    }

}
